import React, { useEffect, useState } from 'react'
import ProForm, { ProFormDigit, ProFormText } from '@ant-design/pro-form';
import { Modal, Button, Image ,message, Skeleton, Cascader} from 'antd';
import AliyunOSSUpload from '@/components/AliyunOSSUpload'
import { UploadOutlined } from '@ant-design/icons';
import { addSlides, showSlides, updateSlides } from '@/services/slides';

export default function CreateOrEdit(props) {
  //表单数据
  const [initialValues, setInitialValues] = useState(undefined)
  const [options, setOptions] = useState([])
  //定义form实例，用来操作表单
  const [formObj] = ProForm.useForm()

  //模态框是否显示
  //操作模态框显示隐藏的方法
  //父组件的传递过来的表格的引用，可以用来操作表格，比如刷新表格
  //要编辑的ID，添加的时候用undefined，只有编辑才有值
  const { isModalVisible, isShowModal, actionRef, editId } = props
  //添加或编辑的文字类型
  const type = editId === undefined ? '添加' : '编辑';
  //文件上传之后设置cover的value
  const setCoverKey = fileKey => {
    //设置表单的值
    formObj.setFieldsValue({ 'img': fileKey })
  }
  //编辑输入内容，设置details字段的value
  const setDetails = content => {
    //设置表单的值
    formObj.setFieldsValue({ 'details': content })
  }
  //提交表单，执行编辑或添加
  const handleSubmit = async values => {
    console.log(values);
    let res = {}
    if (editId === undefined) {
      //发送请求，添加用户
      res = await addSlides({ ...values })
    } else {
      //发送请求，更新用户
      res = await updateSlides(editId, { ...values })
    }
    if (res.status === undefined) {
      message.success(`${type}成功`)
      //刷新表格数据
      actionRef.current.reload()
      //关闭模态框
      isShowModal(false)
    }
  }

  useEffect(() => {
    const getOneSlide = async () => {
      if (editId !== undefined) {
        const res = await showSlides(editId)
        setInitialValues({
          title:res.title,
          url:res.url,
          img_url:res.img_url
        })
      }
    }
    getOneSlide()
  }, [])
  //添加用户
  const createSlides = async values => {
    //发送请求，添加用户
    const res = await addSlides(values)
    if (res.status === undefined) {
      message.success('添加成功')
      //刷新表格数据
      actionRef.current.reload()
      //关闭模态框
      isShowModal(false)
    }
  }
  return (
    <Modal title={`${type}用户`} visible={isModalVisible}
      onCancel={() => isShowModal(false)} footer={null} destroyOnClose={true}>
      {
        //只有在编辑的情况下，并且要显示的数据还没有返回，才显示骨架屏
        initialValues === undefined && editId !== undefined ? <Skeleton active={true} paragraph={{ rows: 2 }} /> :
          <ProForm
            form={formObj}
            initialValues={initialValues}
            onFinish={values => handleSubmit(values)}
          >
           
            <ProFormText
              name="title"
              label="轮播图名称"
              placeholder="请输入轮播图名称"
              rules={[{ required: true, message: '请输入轮播图名称' }]}
            />
            <ProFormText
              name="url"
              label="跳转的URL"
              placeholder="请输入跳转的URL"
              rules={[{ required: true, message: '请输入跳转的URL' }]}
            />


            <ProForm.Item name="img" label="轮播图片" rules={[{ required: true, message: '请上传轮播图片' }]}>
              <div>
                <AliyunOSSUpload accept="image/*" setCoverKey={setCoverKey} showUploadList={true}>
                  <Button icon={<UploadOutlined />}>点击上传轮播图片</Button>
                </AliyunOSSUpload>
                {
                  initialValues === undefined || !initialValues.img_url ? '' :
                    <Image width={100} src={initialValues.img_url} />
                }
              </div>
            </ProForm.Item>
           
          </ProForm>
      }

    </Modal>
  )
}
